package net.example.model;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.oraycn.omcs.shortMessages.AudioMessage;

import net.example.activity.MainActivity;
import net.example.manager.ClientGlobalCache;
import net.example.manager.GGApplication;
import net.example.model.common.ChatBoxElemnetType;
import net.example.model.common.NonText;
import net.example.model.common.RichChatMessage;
import net.example.utils.FileUtils;
import com.oraycn.esbasic.helpers.StringHelper;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

public class ChatInfo implements Serializable {

    /**
     * 接收/发送的信息
     *
     * @param message
     * @return
     */
    public static ChatInfo getChatInfo(String sourceID, String talkingID, String message, boolean isSend,Date sendTime) {
        ChatInfo info = new ChatInfo();
        info.setContent(message);
        info.setFromOrTo(isSend ? 1 : 0);
        info.setTime(sendTime);

        if (isSend) {
            info.setAudienceID(talkingID);
            info.setSpeakerID(GGApplication.getInstance().getCurrentUser().getUserID());
        } else {
            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(talkingID);
            info.setHeadFromUserID(sourceID);
            GGUser user = ClientGlobalCache.getInstance().getUser(sourceID);
            if(user!=null){
                info.setSourceName(user.getCommentName());
            }
        }
        return info;
    }

    //系统消息
    public static ChatInfo getSystemInfo(String audienceID, String message)
    {
        ChatInfo info = new ChatInfo();
        info.setContent(message);
        info.setFromOrTo(0);
        info.setTime(new Date());
        info.setAudienceID(audienceID);
        return info;
    }

    public static ChatInfo getChatInfo4Received(String sourceID,String senderID, RichChatMessage content,boolean isEcho, Date sendTime) {
        String contentStr = content.getText();
        ChatInfo info = new ChatInfo();
        try{ if (content.getNonTextHashMap() != null && content.getNonTextHashMap().size() > 0) {
            StringBuilder newContent = new StringBuilder(contentStr);
            int i = 0;

            for (Map.Entry<Integer, NonText> entry : content.getNonTextHashMap().entrySet()) {
                if (entry.getValue().getChatBoxElementType() == ChatBoxElemnetType.Image) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(entry.getValue().getImage(),
                            0, entry.getValue().getImage().length);
                    String imgPath = FileUtils.saveMyBitmap(bitmap);
//                    info.setImageUrl(imgPath);
                    info = getChatImg(sourceID,senderID, imgPath, false,sendTime);
                } else if(entry.getValue().getChatBoxElementType() == ChatBoxElemnetType.Emotion) {
                    newContent.insert(entry.getKey() + i, "#[face/png/" + MainActivity.staticFacesList.get((int)entry.getValue().getInnerEmotionIndex()) + "]#");
                    i = i + 29;
                }
            }
            contentStr = newContent.toString();
        }
            info.setContent(contentStr);
            info.setFromOrTo(isEcho?1:0);
            info.setTime(sendTime);

            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(senderID);
            info.setHeadFromUserID(sourceID);
            GGUser user= ClientGlobalCache.getInstance().getUser(sourceID);
            if(user!=null) {
                info.setSourceName(user.getCommentName());
            }
        }catch (Exception ex){ex.printStackTrace();}
        info.richChatMessage=content;
        return info;
    }

    public static ChatInfo getChatInfo(String sourceID, String talkingID,RichChatMessage content, boolean isEcho,Date sendTime) {
        String contentStr = content.getText();
        ChatInfo info = new ChatInfo();
        if (content.getNonTextHashMap() != null && content.getNonTextHashMap().size() > 0) {
            StringBuilder newContent = new StringBuilder(contentStr);
            int i = 0;
            for (Map.Entry<Integer, NonText> entry : content.getNonTextHashMap().entrySet()) {
                if (entry.getValue().getChatBoxElementType() == ChatBoxElemnetType.Image) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(entry.getValue().getImage(),
                            0, entry.getValue().getImage().length);

                    String imgPath = FileUtils.saveMyBitmap(bitmap);

                    info = getChatImg(sourceID,talkingID, imgPath, false,sendTime);
                } else if (entry.getValue().getChatBoxElementType() == ChatBoxElemnetType.Emotion) {
                    newContent.insert(entry.getKey() + i, "#[face/png/" + MainActivity.staticFacesList.get((int) entry.getValue().getInnerEmotionIndex()) + "]#");
                    i = i + 29;
                }
            }
            contentStr = newContent.toString();
        }

        info.setContent(contentStr);
        info.setFromOrTo(isEcho ? 1 : 0);
        info.setTime(sendTime);

            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(talkingID);
            info.setHeadFromUserID(sourceID);
            GGUser user = ClientGlobalCache.getInstance().getUser(sourceID);
            if (user != null) {
                info.setSourceName(user.getCommentName());
            }

        info.richChatMessage=content;
        return info;
    }

    public static ChatInfo getChatInfo(String sourceID, String talkingID, AudioMessage audioMessage, boolean isSend,Date sendTime) {
        ChatInfo info = new ChatInfo();
        info.setAudioMessage(audioMessage);
        info.setContent(" ");
        info.setFromOrTo(isSend ? 1 : 0);
        info.setTime(sendTime);

        if (isSend) {
            info.setAudienceID(talkingID);
            info.setSpeakerID(GGApplication.getInstance().getCurrentUser().getUserID());
        } else {
            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(talkingID);

            info.setHeadFromUserID(sourceID);
            GGUser user = ClientGlobalCache.getInstance().getUser(sourceID);
            if (user != null) {
                info.setSourceName(user.getCommentName());
            }
        }
        return info;
    }

    public static ChatInfo getChatImg(String sourceID,String talkingID, String imageUrl, boolean isSend,Date sendTime) {
        ChatInfo info = new ChatInfo();
        info.setImageUrl(imageUrl);
        info.setContent(" ");
        info.setFromOrTo(isSend ? 1 : 0);
        info.setTime(sendTime);

        if (isSend) {
            info.setAudienceID(talkingID);
            info.setSpeakerID(GGApplication.getInstance().getCurrentUser().getUserID());
        } else {
            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(talkingID);

            info.setHeadFromUserID(sourceID);
            GGUser user = ClientGlobalCache.getInstance().getUser(sourceID);
            if (user != null) {
                info.setSourceName(user.getCommentName());
            }
        }
        return info;
    }

    public static ChatInfo getFileInfo(FileMessage file, String talkingID,boolean isSend,Date sendTime) {
        ChatInfo info = new ChatInfo();
        info.setFileMessage(file);
        info.setContent(" ");
        info.setFromOrTo(isSend ? 1 : 0);
        info.setTime(sendTime);

        if (isSend) {
            info.setAudienceID(talkingID);
            info.setSpeakerID(GGApplication.getInstance().getCurrentUser().getUserID());
        } else {
            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            info.setSpeakerID(talkingID);
            info.setHeadFromUserID(talkingID);
        }

        return info;
    }

    public static ChatInfo getChatInfo( AudioMessage content,Date sendTime) {
        ChatInfo info = new ChatInfo();
        try{
            info.setContent(" ");
            info.setFromOrTo(0);
            info.setTime(sendTime);
            info.setAudioMessage(content);
            info.setAudienceID(GGApplication.getInstance().getCurrentUser().getUserID());
            String loginId = content.creatorID;
            String userID=  loginId;
            String [] ids  = loginId.split("#");
            if(ids.length>1) {
                userID = ids[1];
            }
            info.setSpeakerID(userID);
            GGUser user = ClientGlobalCache.getInstance().getUser(userID);
            if (user != null) {
                info.setSourceName(user.getCommentName());
            }
            info.setHeadFromUserID(userID);
        }catch (Exception ex){ex.printStackTrace();}
        return info;
    }






    public final static int chat_img_item = 0;
    public final static int chat_file_item = 1;
    public final static int chat_lv_item = 2;
    public final static int chat_audio_item = 3;
    public final static int chat_vibration_item = 4;
    private transient RichChatMessage richChatMessage=null;
    private String headFromUserID;//头像的 用户ID
//    private int iconFromResId;
    private String iconFromUrl = "";
    private String content = " ";
    private Date time;
    private int fromOrTo;// 0 是收到的消息，1是发送的消息
    private String ImageUrl = "";
    private String SpeakerID="";
    private String SourceName="";
    private String AudienceID="";
    private String fileStateMsg;
    private int voice;
    private int video;
    private boolean fileHasDown = false;
    private FileMessage fileMessage;
    private int messageType;
    private long autoID=0;//DB中对应的autoID
    private boolean isGroupChat;
    private boolean isRead;
    private boolean acceptFile=true;
    private boolean isFromSession= false;
    public String snapchatMessageID ="";//阅后即焚消息ID
    public int snapchatMessageShowSeconds=10;//阅后即焚消息显示秒数

    public void setRichChatMessage(RichChatMessage richChatMessage)
    {
        this.richChatMessage=richChatMessage;
    }
    public RichChatMessage getRichChatMessage()
    {
        return this.richChatMessage;
    }

    public void setFileStateMsg(String msg){
        this.fileStateMsg = msg;
    }

    public String getFileStateMsg( ) {
        return fileStateMsg;
    }

    public boolean isFromSession() {
        return isFromSession;
    }

    public void setIsFromSession(boolean isFromSession) {
        this.isFromSession = isFromSession;
    }
    public boolean getAcceptFile() {
        return acceptFile;
    }

    public void setAcceptFile(boolean acceptFile) {
        this.acceptFile = acceptFile;
    }


    public String getSourceName() {
        return SourceName;
    }

    public void setSourceName(String speakerName) {
        this.SourceName = speakerName;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getSpeakerID() {
        if(StringHelper.isNullOrEmpty(SpeakerID))
        {
            return "";
        }
        return SpeakerID;
    }

    public void setSpeakerID(String speakerID) {
        this.SpeakerID = speakerID;
    }

    public String getAudienceID() {
        return AudienceID;
    }

    public void setAudienceID(String audienceID) {
        this.AudienceID = audienceID;
    }

    public boolean isGroupChat() {
        return isGroupChat;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setIsRead(boolean isRead) {
        this.isRead = isRead;
    }

    public void setIsGroupChat(boolean isGroupChat) {
        this.isGroupChat = isGroupChat;
    }

    public boolean isFileHasDown() {
        return fileHasDown;
    }

    public void setFileHasDown(boolean fileHasDown) {
        this.fileHasDown = fileHasDown;
    }

    public FileMessage getFileMessage() {
        return fileMessage;
    }

    public void setFileMessage(FileMessage fileMessage) {
        this.fileMessage = fileMessage;
    }

//    public int getIconFromResId() {
//        return iconFromResId;
//    }
//
//    public void setIconFromResId(int iconFromResId) {
//        this.iconFromResId = iconFromResId;
//    }

    public void setHeadFromUserID(String userID)
    {
        this.headFromUserID =userID;
    }

    public String getHeadFromUserID()
    {
        return this.headFromUserID;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 0 是收到的消息，1是发送的消息
     * */
    public int getFromOrTo() {
        return fromOrTo;
    }

    /**
     * 0 是收到的消息，1是发送的消息
     * */
    public void setFromOrTo(int fromOrTo) {

        this.fromOrTo = fromOrTo;
    }

    public String getImageUrl() {
        return ImageUrl;
    }

    public void setImageUrl(String imageUrl) {
        ImageUrl = imageUrl;
    }

    private AudioMessage audioMessage;

    public AudioMessage getAudioMessage() {
        return audioMessage;
    }

    public void setAudioMessage(AudioMessage audioMessage) {
        this.audioMessage = audioMessage;
    }

    public void setAutoID(long autoID)
    {
        this.autoID=autoID;
    }
    public long getAutoID()
    {
        return this.autoID;
    }

}
